<!-- eslint-disable vue/valid-attribute-name -->
<template>
  <div>
    <category :scene="scene" />
    <el-card style="margin: 10px 0px">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table border style="width: 100%; margin: 10px 0px" :data="spuArr">
          <el-table-column
            prop=""
            label="序号"
            type="index"
            width="85px"
            :align="'center'"
          />
          <el-table-column prop="spuName" label="SPU名称" />
          <el-table-column
            prop="description"
            show-overflow-tooltip
            label="SPU描述"
          />
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SPU"
              />
              <el-button
                type="warning"
                size="small"
                icon="Edit"
                title="编辑SPU"
                @click="editSpu(row)"
              />
              <el-button
                type="info"
                size="small"
                icon="QuestionFilled"
                title="查看SPU"
              />
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                title="删除SPU"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[1, 3, 5, 7]"
          small="small"
          background="true"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          @change="handleSizeChange"
        />
      </div>
      <!--spu子组件-->
      <spuForm
        ref="spuFormData"
        v-show="scene == 1"
        @changeScene="changeScene"
      />
      <!--sku子组件-->
      <skuFrom v-show="scene == 2" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { getSpuPage } from "../../../api/product/spu/index"
import spuForm from "./spuForm.vue"
import skuFrom from "./skuFrom.vue"
import {
  ResponseSpuData,
  Records,
  SpuData,
} from "../../../api/product/spu/type"
import { ref, watch } from "vue"
import useCategoryStore from "../../../store/modules/category"
let scene = ref<number>(0) //显示已有spu 1 添加或修改spu 2添加sku
let currentPage = ref<number>(1)
let pageSize = ref<number>(3)
let total = ref<number>(0)
let spuArr = ref<Records>([])
let spuFormData = ref<any>()

let categoryStore = useCategoryStore()

//监听仓库三级分类id
watch(
  () => categoryStore.c3Id,
  () => {
    // attr.value = []
    if (!categoryStore.c3Id) return
    getSpuList(currentPage.value, pageSize.value)
  },
)

const getSpuList = async (page_no: number, page_size: number) => {
  let result: ResponseSpuData = await getSpuPage(
    page_no,
    page_size,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    spuArr.value = result.data.records
    total.value = result.data.total
  }
}
const handleSizeChange = () => {
  getSpuList(currentPage.value, pageSize.value)
}

const addSpu = () => {
  scene.value = 1
}

const editSpu = (row: SpuData) => {
  scene.value = 1
  //调用子组件实力方法获取网站已有的spu数据
  spuFormData.value.initHasSpuData(row)
}

const changeScene = (num: number) => {
  scene.value = num
}
</script>

<style scoped></style>
